package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.domain.dao.ProductSerialFlowDao;
import com.xbongbong.saas.domain.entity.ProductSerialFlowEntity;
import com.xbongbong.saas.model.ProductSerialFlowModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service("productSerialFlowModel")
public class ProductSerialFlowModelImpl implements ProductSerialFlowModel {

    @Resource
    private ProductSerialFlowDao productSerialFlowDao;

    @Override
    public Integer insert(ProductSerialFlowEntity entity) throws XbbException {
        return productSerialFlowDao.insert(entity);
    }

    @Override
    public Integer update(ProductSerialFlowEntity entity) throws XbbException {
        return productSerialFlowDao.update(entity);
    }

    @Override
    public List<ProductSerialFlowEntity> findEntitys(Map<String, Object> param) {
        return productSerialFlowDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return productSerialFlowDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public void insertBatch(List<ProductSerialFlowEntity> productSerialFlowEntitys) {
        long now = DateTimeUtil.getInt();
        for (ProductSerialFlowEntity entity : productSerialFlowEntitys) {
            entity.setAddTime(now);
            entity.setUpdateTime(now);
        }
        productSerialFlowDao.insertBatch(productSerialFlowEntitys);
    }

    @Override
    public Integer deleteBatch(List<Long> refIdIn, String corpid, Integer businessType, List<Long> enableSerialProductIds) {
        return productSerialFlowDao.deleteBatch(refIdIn, corpid, businessType, enableSerialProductIds);
    }

    @Override
    public Integer deleteBatchById(List<Long> idIn, String corpid) {
        if (CollectionUtils.isEmpty(idIn)) {
            return 0;
        }
        return productSerialFlowDao.deleteBatchById(idIn, corpid);
    }

    @Override
    public ProductSerialFlowEntity getLastSerialFlow(String corpid, String seqKey, Long refId) throws XbbException {
        return productSerialFlowDao.getLastSerialFlow(corpid, seqKey, refId);
    }

    @Override
    public void delByRefProductAndBusinessType(String corpid, Long refProductId, int businessType) {
        productSerialFlowDao.delByRefProductAndBusinessType(corpid, refProductId, businessType);
    }

    @Override
    public List<ProductSerialFlowEntity> getBySeqKey(String seqKey, String corpid) {
        return productSerialFlowDao.getBySeqKey(seqKey, corpid);
    }

    @Override
    public List<ProductSerialFlowEntity> getFlowByRefIdAndBusiness(List<Long> refIdIn, Integer businessType, String corpid) {
        return productSerialFlowDao.getFlowByRefIdAndBusiness(refIdIn, businessType, corpid);
    }

    @Override
    public Integer updateBatch(List<ProductSerialFlowEntity> updateSerialFlowList, String corpid) {
        if (CollectionUtils.isEmpty(updateSerialFlowList)) {
            return 0;
        }
        return productSerialFlowDao.updateBatch(updateSerialFlowList, corpid);
    }

    @Override
    public Integer deleteByRefProductId(String corpid, Long refId, Collection<Long> refProductIdIn, Integer businessType) {
        if (CollectionUtils.isEmpty(refProductIdIn)) {
            return 0;
        }
        return productSerialFlowDao.deleteByRefProductId(corpid, refId, refProductIdIn, businessType);
    }

    @Override
    public List<ProductSerialFlowEntity> getLastSerialFlowByProductId(Map<String, Object> param) throws XbbException{
        return productSerialFlowDao.getLastSerialFlowByProductId(param);
    }
}
